home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 November / Macworld Nov ’95.toast / Developers / Selection ƒ 2.5 / >heap next >
Encoding:
Text File  |  1994-11-06  |  2.0 KB  |  71 lines  |  [TEXT/MSET]

  1. \ 01Dec93 DBH  redefined >dispose so killer is not reqired.  saved 46 bytes
  2.  
  3. (*
  4. >heap and >dispose allow us to dynamically create nameless objects from
  5. the heap in the same way Neon did.  The advantage to using this method over,
  6. for example OBJHANDLE, is that we can simply send messages straight to the
  7. object if we store the returned pointer in a value or local variable
  8. (we need not first send the obj: method first).  Of course since we are
  9. locking a handle we should be careful not to create too many of these
  10. objects such that the heap is fragmented.  This should not be a problem for
  11. relatively small or transient objects created with >heap.
  12. *)
  13.  
  14. : PUSHA0    $ 2D08 w,  ;    immediate    \    move.l    a0,-(a6)
  15. : POPA0        $ 205E w,  ;    immediate    \    move.l    (a6)+,a0
  16.  
  17. \ This stuff allows Neon pointer type objects in Mops to allow a programmer
  18. \ to choose whether to use handle type objects after the conversion to
  19. \ Mops is complete.
  20.  
  21. ObjHandle newObjVar    \ temporary handle to create new obj ala Mops
  22.  
  23. \ handle killer    \ used in killhandle
  24.  
  25. \ : KILLHANDLE    ( handle -- )    \ new version.  dbh 4/20/92
  26. \    put: killer
  27. \    release: killer ;
  28.  
  29. : >heap { ^class \ objHdl objLen -- ^obj }
  30.  
  31.     \ pinched from NEWOBJ:, but save obj length for erase
  32.     ^class cl>len 8 + dup -> objLen new: newObjVar
  33.  
  34.     moveHi: newObjVar            \ debatable
  35.     get: newObjVar -> objHdl    \ save handle
  36.     
  37.     ptr: newObjVar objLen erase    \ clear it like Neon
  38.  
  39.     \ let mops do its thing
  40.     ^class obj: newObjVar make_obj
  41.  
  42.     \ do not unlock, cannot use newObjVar
  43.     \ as classinit: may cause >heap to be re-entered
  44.     objHdl @   8 +
  45. ;
  46.  
  47. : >dispose ( ^obj -- )        \ note resolution of forward definition
  48.     8 - popA0 call RecoverHandle pushA0
  49.     ?dup
  50.     IF
  51.         put: newObjVar    \ this is a handle method
  52.         newObjVar release: handle    \ note message to class to override objhandle release:  !!
  53.     THEN
  54. ;
  55.  
  56. endload
  57.  
  58. *** EXAMPLE USE
  59.  
  60. ' null value heapRect    \ storage for our object
  61.  
  62. : go
  63.     ['] rect+ >heap -> heapRect    \ create a rect+ from the heap
  64.     draw: heapRect        \ look ma, no obj:
  65.     heapRect >dispose    \ return memory to the heap, must not use heapRect now
  66.     ;
  67.  
  68.  
  69.  
  70.  
  71.